/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.BooleanTypeEnum;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.api.entity.GoodsBanner;
import com.pig4cloud.pigx.shop.api.entity.PointSpu;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import com.pig4cloud.pigx.shop.service.PointSpuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分商品
 *
 * @author pigx code generator
 * @date 2023-12-18 17:29:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/point/spu")
@Tag(description = "point_spu", name = "积分商品管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PointSpuController {

	private final PointSpuService pointSpuService;

	private final GoodsMaterialService materialService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param pointSpu 积分商品
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getPointSpuPage(Page page, PointSpu pointSpu) {
		return R.ok(pointSpuService.getPage(page, pointSpu));
	}

	/**
	 * 通过id查询积分商品
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		PointSpu pointSpu = pointSpuService.getById(id);
		if (!StringUtils.isEmpty(pointSpu.getBannerImages())) {
			pointSpu.setBannerImages(materialService.convertImageIdToUrl(pointSpu.getBannerImages()));
		}
		if (!StringUtils.isEmpty(pointSpu.getDetailImages())) {
			pointSpu.setDetailImages(materialService.convertImageIdToUrl(pointSpu.getDetailImages()));
		}
		return R.ok(pointSpu);
	}

	/**
	 * 新增积分商品
	 * @param pointSpu 积分商品
	 * @return R
	 */
	@Operation(summary = "新增积分商品", description = "新增积分商品")
	@SysLog("新增积分商品")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('point_spu_add')")
	public R save(@RequestBody PointSpu pointSpu) {
		return R.ok(pointSpuService.save(pointSpu));
	}

	/**
	 * 修改积分商品
	 * @param pointSpu 积分商品
	 * @return R
	 */
	@Operation(summary = "修改积分商品", description = "修改积分商品")
	@SysLog("修改积分商品")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('point_spu_edit')")
	public R updateById(@RequestBody PointSpu pointSpu) {
		return R.ok(pointSpuService.updateById(pointSpu));
	}

	/**
	 * 通过id删除积分商品
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除积分商品", description = "通过id删除积分商品")
	@SysLog("通过id删除积分商品")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('point_spu_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(pointSpuService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param pointSpu 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('point_spu_export')")
	public List<PointSpu> export(PointSpu pointSpu) {
		return pointSpuService.list(Wrappers.query(pointSpu));
	}

	/**
	 * 获取积分商城的推荐列表
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getPointRecommendList")
	public R getPointRecommendList() {
		LambdaQueryWrapper<PointSpu> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PointSpu::getHasRecommend, BooleanTypeEnum.TRUE.getType());
		lqw.eq(PointSpu::getDelFlag, DeleteFlagEnum.VALID.getType());
		lqw.eq(PointSpu::getStatus, BooleanTypeEnum.TRUE.getType());
		lqw.orderBy(true, true, PointSpu::getSort);
		List<PointSpu> items = pointSpuService.list(lqw);
		items.forEach(item -> {
			if (!StringUtils.isEmpty(item.getBannerImages())) {
				item.setBannerImages(materialService.convertImageIdToUrl(item.getBannerImages()));
			}
			if (!StringUtils.isEmpty(item.getDetailImages())) {
				item.setDetailImages(materialService.convertImageIdToUrl(item.getDetailImages()));
			}
		});
		return R.ok(items);
	}

	/**
	 * 分页获取积分商城商品
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getPointSpuPage")
	public R getPointSpuPageByCategoryId(Page page, PointSpu pointSpu) {
		pointSpu.setStatus(BooleanTypeEnum.TRUE.getType());
		return R.ok(pointSpuService.getPage(page, pointSpu));
	}

	/**
	 * c端获取详情
	 * @param pointSpu
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getPointSpuDetail")
	public R getPointSpuDetail(PointSpu pointSpu) {
		return this.getById(pointSpu.getId());
	}

}
